package com.homegoods.discover.ui.fragment;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.homegoods.discover.R;
import com.homegoods.discover.presenter.VideoPresenter;
import com.kedll.library.fragment.KRefreshListFragment;
import com.kedll.library.mvp.BasePresenter;
import com.kedll.library.util.MyUtils;

import cn.jzvd.JZMediaManager;
import cn.jzvd.Jzvd;
import cn.jzvd.JzvdMgr;

/**
 * @author QiQi
 * @ClassName: VideoFragment
 * @date 2018/9/10
 * @description 视频
 */
public class VideoFragment extends KRefreshListFragment {
    @Override
    protected void init() {
        super.init();
        Jzvd.setVideoImageDisplayType(Jzvd.VIDEO_IMAGE_DISPLAY_TYPE_ORIGINAL);
        list.addItemDecoration(new SpacesItemDecoration(5));
        list.addOnChildAttachStateChangeListener(new RecyclerView.OnChildAttachStateChangeListener() {
            @Override
            public void onChildViewAttachedToWindow(View view) {

            }

            @Override
            public void onChildViewDetachedFromWindow(View view) {
                Jzvd jzvd = view.findViewById(R.id.videoplayer);
                if (jzvd != null && jzvd.jzDataSource.containsTheUrl(JZMediaManager.getCurrentUrl())) {
                    Jzvd currentJzvd = JzvdMgr.getCurrentJzvd();
                    if (currentJzvd != null && currentJzvd.currentScreen != Jzvd.SCREEN_WINDOW_FULLSCREEN) {
                        Jzvd.releaseAllVideos();
                    }
                }
            }
        });
    }

    @Override
    public void onUserInvisible() {
        super.onUserInvisible();
        handler.postDelayed(Jzvd::goOnPlayOnPause, 100);
    }

    @Override
    public void onUserVisible() {
        super.onUserVisible();
        handler.postDelayed(Jzvd::goOnPlayOnResume, 100);
    }

    @Override
    protected BasePresenter getPresenter() {
        return new VideoPresenter(this);
    }

    @Override
    protected int getLayout() {
        return R.layout.pullable_listview;
    }

    private final static class SpacesItemDecoration extends RecyclerView.ItemDecoration {

        private int space;
        private Paint mPaint;

        SpacesItemDecoration(int space) {
            //屏幕宽度
            this.space = MyUtils.dp2px(space);
            mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
            mPaint.setColor(Color.parseColor("#f6f6f6"));
            mPaint.setStyle(Paint.Style.FILL);
        }

        @Override
        public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
            super.onDraw(c, parent, state);
            final int left = parent.getPaddingLeft();
            final int right = parent.getWidth() - parent.getPaddingRight();
            final int childCount = parent.getChildCount();

            for (int i = 0; i < childCount; i++) {
                final View child = parent.getChildAt(i);
                final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
                final int top = child.getBottom() + params.bottomMargin;
                final int bottom = top + space;
                c.drawRect(left, top, right, bottom, mPaint);
            }
        }

        @Override
        public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
            outRect.set(0, 0, 0, space);
        }
    }
}
